package com.igoal.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringEscapeUtils;

import com.igoal.entity.User;
import com.igoal.entity.UserInfo;
import com.igoal.entity.UserStatus;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.StringUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;

public class FriendDTO extends BaseDTO<User> {
	private String avatar;
	private Boolean isLoggined = false;
	private Integer numberGoals;
	private String username;
	private String status;
	private String fullName;
	private Integer numberDoingGoals;
	private Integer numberFinishedGoals;
	private Integer numberOfAllEntries;
	
	public Integer getNumberDoingGoals(){
		return numberDoingGoals;
	}
	
	public Integer getNumberFinishedGoals(){
		return numberFinishedGoals;
	}
	
	public Integer getNumberOfAllEntries(){
		return numberOfAllEntries;
	}
	
	public void setNumberDoingGoals(Integer numberDoingGoals){
		this.numberDoingGoals = numberDoingGoals;
	}
	
	public void setNumberFinishedGoals(Integer numberFinishedGoals){
		this.numberFinishedGoals = numberFinishedGoals;
	}
	public void setNumberOfAllEntries(Integer numberOfAllEntries){
		this.numberOfAllEntries = numberOfAllEntries;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Boolean getLoggined() {
		return isLoggined;
	}

	public void setLoggined(Boolean isLoggined) {
		this.isLoggined = isLoggined;
	}

	public Integer getNumberGoals() {
		return numberGoals;
	}

	public void setNumberGoals(Integer numberGoals) {
		this.numberGoals = numberGoals;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		if(status != null){
			status = StringEscapeUtils.escapeHtml(status);
		}
		this.status = status;
	}
	
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "updateDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updatedSince = CalendarUtils.calculateMinutesDifferentBeforeNow(date);
		}
	}

	@SuppressWarnings("unchecked")
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "userStatuses")
	public void initStatusFromEntity(Object entity) {
		if (entity != null && entity instanceof Set<?>) {
			Set<UserStatus> userStatuses = (Set<UserStatus>) entity;
			if (userStatuses.size() > 0) {
				List<UserStatus> listStatuses = new ArrayList<UserStatus>(userStatuses);
				UserStatus userStatus = listStatuses.get(0);
				setStatus(userStatus.getUserStatus());
			}
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "userInfo")
	public void initDisplayNameFromEntity(Object entity) {
		if (entity != null && entity instanceof UserInfo) {
			UserInfo info = (UserInfo) entity;
			String firstName = info.getFirstName();
			String midleName = info.getMiddleName();
			String lastName = info.getLastName();
			String username = info.getUser().getUsername();

			fullName = StringUtils.buildDisplayName(username, firstName,
					midleName, lastName);
		}
	}
	
	@Override
	public int hashCode() {
		return this.username.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj != null && obj instanceof FriendDTO) {
			FriendDTO that = (FriendDTO) obj;

			if (this.username == null)
				return false;

			return this.username.equals(that.username);
		}

		return false;
	}
}